package look4ss.aplicativo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;
import com.github.chrisbanes.photoview.PhotoView;
import com.sdsmdg.tastytoast.TastyToast;
import com.squareup.picasso.Picasso;
import look4ss.aplicativo.fragments.AbaInfos;

public class ActivityDetalhes extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detalhes);
        Toolbar barraFerramentas = findViewById(R.id.barra1);
        setSupportActionBar(barraFerramentas);
        barraFerramentas.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ActionBar ABr = getSupportActionBar();
        if (ABr != null) {
            ABr.setTitle(R.string.detalhes_titulo);
            ABr.setDisplayHomeAsUpEnabled(true);
        }
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_textos,
                    new Info()).commit();
        }
    }

    public static class Info extends Fragment {
        public static String nome = "";
        public static String descricao = "";
        public static String id = "";
        public static String imagem = "";
        public static int num;
        private VideoView VideoLugar;
        private PhotoView ImagemLugar;
        private ViewPager vPager;
        private TabLayout TL;
        private ProgressBar barraDeLoading;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup
                container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.conteudo_activitydetalhes, container,
                    false);
            vPager = v.findViewById(R.id.pager);
            TL = v.findViewById(R.id.layout_aba);
            vPager.setAdapter(new AdapterAbas(getChildFragmentManager()));
            TL.setupWithViewPager(vPager);
            barraDeLoading = v.findViewById(R.id.imagemCarregando);

            if (imagem.contains("mp4")) {
                VideoLugar = v.findViewById(R.id.video);
                carregarVideo();
                return v;
            }else{
                ImagemLugar = v.findViewById(R.id.imagem);
                carregarImagem();
                return v;
            }
        }

        class AdapterAbas extends FragmentPagerAdapter {
            private AdapterAbas(FragmentManager fm) {
                super(fm);
            }

            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public Fragment getItem(int i) {
                return new AbaInfos();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return getString(R.string.detalhes_titulo);

            }
        }

        private void carregarImagem() {
            ImagemLugar.setVisibility(View.VISIBLE);
            setHasOptionsMenu(true);
            Picasso.get().load(imagem).fit().into(ImagemLugar, new com.squareup.picasso.Callback() {
                public void onSuccess() {
                    barraDeLoading.setVisibility(View.GONE);
                }
                public void onError(Exception ex) {
                    TastyToast.makeText(getActivity(), getString(R.string.erro_carregar_imagem), Toast.LENGTH_LONG, TastyToast.ERROR);
                    barraDeLoading.setVisibility(View.GONE);
                }
            });
            ImagemLugar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(getActivity(), ActivityImagem.class));

                }
            });
        }
        private void carregarVideo() {
            VideoLugar.setVisibility(View.VISIBLE);
            MediaController mc = new MediaController(getActivity());
            mc.setAnchorView(VideoLugar);
            mc.setMediaPlayer(VideoLugar);
            Uri video = Uri.parse(imagem);
            VideoLugar.setMediaController(mc);
            VideoLugar.setVideoURI(video);
            VideoLugar.requestFocus();
            VideoLugar.start();
            barraDeLoading.setVisibility(View.GONE);
        }
        }
    }